const app = getApp()
Page({
  data: {
    CDNPATH:app.globalData.CDNPATH,
    currentTab: '',
    sceneList: [],
    tempsceneName:'',
    sceneName:'',
    contentResIds:[],
    contentCN:[],
    tabList:[],
    phoneType:app.globalData.phoneType
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let contentResIds = app.globalData.contentResIds||[]
    let contentCN = app.globalData.contentCN||[]
    this.setData({
      contentResIds:contentResIds,
      contentCN:contentCN
    })
    // 页面初始化 options为页面跳转所带来的参数
    this.getClassify()
    this.getSceneList()
  },
  searchByname(){
    this.choseSave()
    this.setData({
      currentTab: '',
      sceneName:this.data.tempsceneName
    })
    this.getSceneList()
  },
  getClassify() {
    app.api.post({
      url: 'scene_trainingType_getAll',
    }).then(res=>{
      const arr = res || []
      arr.unshift({
        name:'全部',
        id:''
      })
      this.setData({
        tabList:res || []
      })
    })
  },
  getSceneList () {
    let data={
      sceneName:this.data.sceneName,
      trainingTypeId:this.data.currentTab
    }
    app.api.post({
      url: 'scene_manage_getScenesByTrainingTypeIdAndSceneName',
      data
    }).then(res => {
      console.log(res,'res')
      let list = res||[]
      for (let i = 0; i < list.length; i++) {
        let arr_chose = this.data.contentResIds
        list[i].checked = false
        if(arr_chose.length>0){
          arr_chose.includes(list[i].sceneId) && (list[i].checked = true)
          console.log(arr_chose.includes(list[i].sceneId),arr_chose[0],list[i].sceneId)
        }
      }
      this.setData({
        sceneList: list
      })
    })
  },
  //点击切换
  clickTab: function (e) {
    console.log(e)
    let that = this;
    this.choseSave()
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      this.getSceneList()
    }
  },
  checkedTap: function (e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let sceneList = this.data.sceneList
    sceneList.forEach((item)=>{
      item.sceneId === id && (item.checked = !item.checked)
    })
    this.setData({
      "sceneList": sceneList
    })
  },
  //保存选择的radio
  choseSave(){
    let arr = this.data.sceneList
    let arr_chose = this.data.contentResIds
    let arr_cn = this.data.contentCN
    // console.log(this.data.contentResIds)
    for (let i = 0; i < arr.length; i++) {
      if(arr[i].checked===true){
        arr_chose.indexOf(arr[i].sceneId)===-1 && arr_chose.push(arr[i].sceneId) &&
        arr_cn.push(arr[i].sceneName)  
      }
    }
    this.setData({
      contentResIds:arr_chose
    })
  },
  //确定按钮
  courseSubmit(){
    this.choseSave()
    app.globalData.contentResIds = this.data.contentResIds
    app.globalData.contentCN = this.data.contentCN
    app.globalData.dataUpdata = 'pending'
    console.log(app.globalData)
    wx.navigateBack()
    // wx.redirectTo({
    //   url: '/myself/pages/train-manage/courseschose/courseschose'
    // })
  }
})
